package com.tongtech.space.module.login.service;

import com.tongtech.space.common.pojo.UserInfo;
import com.tongtech.space.common.utils.TisRequest;
import com.tongtech.space.module.login.model.LoginDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 登录相关Service
 *
 * @author jinjiayu
 * @since 2020/12/18
 */
@Slf4j
@Service
public class LoginService {
    @Autowired
    private TisRequest tisRequest;

    /**
     * 根据tokenSNO和用户类型获取用户信息
     *
     * @param loginDto
     * @return UserInfo
     * @author jinjiayu
     */
    public UserInfo getUserInfo(LoginDto loginDto) {
       /* UserInfo userInfo = null;
        // TODO
        try {
            if (loginDto.getUserType() == UserTypeEnums.NATURE_PERSON.getCode()) {
                // 将个人用户数据封装为userInfo对象
                userInfo = new UserInfo();
                userInfo.setUserId();
                userInfo.setUserType(String.valueOf(UserTypeEnums.NATURE_PERSON.getCode()));
                userInfo.setUserName();
                userInfo.setCardId();
                userInfo.setPhone();
                userInfo.setCardType();
                userInfo.setUserLevel();
                userInfo.setUserRegion(loginDto.getAreaCode());
                userInfo.setSex(null);
                if (StringUtils.isEmpty(userInfo.getUserId())) {
                    log.error("用户userid为空!");
                    return null;
                }

                // 将个人用户数据保存到TIS user表
                List<User> users = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_USER, UserId.createLow(userInfo.getUserId()),
                        new TypeReference<List<User>>() {});
                if (CollectionUtils.isEmpty(users)) {
                    User user = new User();
                    user.setUserid();
                    user.setUsertype(Constants.PERSON_TYPE);
                    user.setName();
                    user.setAccount();
                    user.setCertid();
                    user.setCerttype();
                    user.setLinkphone();
                    tisRequest.addByJsonObject(APP_MODEL_USER, (JSONObject)JSON.toJSON(user));
                }

                // 将个人用户数据保存到TIS rkuser表
                List<RkUser> rkUsers = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_RKUSER, UserId.createLow(userInfo.getUserId()),
                        new TypeReference<List<RkUser>>() {});
                if (CollectionUtils.isEmpty(rkUsers)) {
                    RkUser rkUser = new RkUser();
                    rkUser.setUserid();
                    rkUser.setXm();
                    rkUser.setZjzlhz();
                    rkUser.setZjhm();
                    rkUser.setSjhm();
                    tisRequest.addByJsonObject(APP_MODEL_RKUSER, (JSONObject)JSON.toJSON(rkUser));
                }


            } else if (loginDto.getUserType() == UserTypeEnums.LEGAL_PERSON.getCode()) {
                if (loginDto.isH5()) {
                    // 将法人用户数据封装为userInfo对象
                    userInfo = new UserInfo();
                    userInfo.setUserId(loginDto.getXydm());
                    userInfo.setUserType(Constants.COMPANY_TYPE);
                    userInfo.setUserName(loginDto.getQymc());
                    userInfo.setUserRegion(loginDto.getAreaCode());
                    userInfo.setUnionCode(loginDto.getXydm());
                    userInfo.setCorpName(loginDto.getQymc());
                    if (StringUtils.isEmpty(userInfo.getUserId())) {
                        log.error("用户userid为空!");
                        return null;
                    }

                    // 将法人用户数据保存到TIS company表
                    List<Company> companies = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_COMPANY, UserId.createLow(userInfo.getUserId()),
                            new TypeReference<List<Company>>() {});
                    if (CollectionUtils.isEmpty(companies)) {
                        Company company = new Company();
                        company.setUserid(loginDto.getXydm());
                        company.setUsertype(Constants.COMPANY_TYPE);
                        company.setCreditcode(loginDto.getXydm());
                        tisRequest.addByJsonObject(APP_MODEL_COMPANY, (JSONObject)JSON.toJSON(company));
                    }

                    // 将个人用户数据保存到TIS corpinfo表
                    List<Corpinfo> corpinfos = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_CORPINFO, UserId.createLow(userInfo.getUserId()),
                            new TypeReference<List<Corpinfo>>() {});
                    if (CollectionUtils.isEmpty(corpinfos)) {
                        Corpinfo corpinfo = new Corpinfo();
                        corpinfo.setUserid(loginDto.getXydm());
                        corpinfo.setUniscid(loginDto.getXydm());
                        corpinfo.setCorpname(loginDto.getQymc());
                        tisRequest.addByJsonObject(APP_MODEL_CORPINFO, (JSONObject)JSON.toJSON(corpinfo));
                    }
                } else {
                    // 将法人用户数据封装为userInfo对象
                    userInfo = new UserInfo();
                    userInfo.setUserId();
                    userInfo.setUserType(Constants.COMPANY_TYPE);
                    userInfo.setUserName();
                    userInfo.setPhone();
                    userInfo.setCardType(null);
                    userInfo.setUserRegion(loginDto.getAreaCode());
                    userInfo.setCardId();
                    userInfo.setUnionCode();
                    userInfo.setCorpName();
                    if (StringUtils.isEmpty(userInfo.getUserId())) {
                        log.error("用户userid为空!");
                        return null;
                    }

                    // 将法人用户数据保存到TIS company表
                    List<Company> companies = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_COMPANY, UserId.createLow(userInfo.getUserId()),
                            new TypeReference<List<Company>>() {});
                    if (CollectionUtils.isEmpty(companies)) {
                        Company company = new Company();
                        company.setUserid();
                        company.setUsertype(Constants.COMPANY_TYPE);
                        company.setLawperson();
                        company.setLinkmobile();
                        company.setCreditcode();
                        tisRequest.addByJsonObject(APP_MODEL_COMPANY, (JSONObject)JSON.toJSON(company));
                    }

                    // 将个人用户数据保存到TIS corpinfo表
                    List<Corpinfo> corpinfos = tisRequest.queryDataFromTisByUserId(APP_QUERYVIEW_CORPINFO, UserId.createLow(userInfo.getUserId()),
                            new TypeReference<List<Corpinfo>>() {});
                    if (CollectionUtils.isEmpty(corpinfos)) {
                        Corpinfo corpinfo = new Corpinfo();
                        corpinfo.setUserid();
                        corpinfo.setUniscid();
                        corpinfo.setCorpname();
                        corpinfo.setPersonname();
                        corpinfo.setTelephone();
                        corpinfo.setPersoncertcode();
                        tisRequest.addByJsonObject(APP_MODEL_CORPINFO, (JSONObject)JSON.toJSON(corpinfo));
                    }
                }

            }
        } catch (Exception e) {
            log.error("获取用户信息失败!" + e.getMessage());
        }
        return userInfo;*/
        return null;
    }

    @Transactional
    public void test(){

    }

}
